package org.mx.service.server;

import org.mx.StringUtils;
import org.mx.TypeUtils;
import org.springframework.beans.factory.annotation.Value;

import lombok.Getter;
import lombok.Setter;

/**
 * 描述： RESTful服务配置对象
 *
 * @author john peng
 * Date time 2018/7/18 下午1:17
 */
public class ServletServerConfigBean extends HttpServerConfigBean {
    @Value("${servlet.enabled:false}")
    @Getter
    private boolean enabled;
	@Value("${restful.host:0.0.0.0}")
	@Getter
	@Setter
	private String host;
    @Value("${servlet.port:9999}")
    @Getter
    @Setter
    private int port;
    @Value("${servlet.threads:100}")
    @Getter
    private int threads;
    @Value("${servlet.security:false}")
    @Getter
    private boolean security;
    @Value("${servlet.security.keystore:}")
    @Getter
    private String keystorePath;
    @Value("${servlet.security.keystorePassword:}")
    @Getter
    private String keystorePassword;
    @Value("${servlet.security.keyManagerPassword:}")
    @Getter
    private String keyManagerPassword;
    @Value("${servlet.security.keystoreType:JKS}")
    @Getter
    private String keystoreType;
    @Value("${servlet.security.keyAlias:jetty}")
    @Getter
    private String keyAlias;
	@Value("${servlet.security.keyManagerFactoryAlgorithm:SunX509}")
	@Getter
	private String keyManagerFactoryAlgorithm;
	@Value("${servlet.security.maxCertPathLength:-1}")
	@Getter
	private int maxCertPathLength;
	@Value("${servlet.security.validatCerts:true}")
	@Getter
	private boolean validateCerts;
	@Value("${servlet.security.validatePeerCerts:false}")
	@Getter
	private boolean validatePeerCerts;
	@Value("${servlet.security.needClientAuth:false}")
	@Getter
	private boolean needClientAuth;
	@Value("${servlet.security.wantClientAuth:false}")
	@Getter
	private boolean wantClientAuth;

    @Value("${servlet.idleTimeoutSecs:300}")
    @Getter
    private long idleTimeoutSecs;
    @Value("${servlet.outputSize:32K}")
    private String outputSizeString;
    @Value("${servlet.requestHeaderSize:8K}")
    private String requestHeaderSizeString;
    @Value("${servlet.responseHeaderSize:8K}")
    private String responseHeaderSizeString;

    @Value("${servlet.security.cipherSuites:TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA384,TLS_DHE_RSA_WITH_AES_128_GCM_SHA384,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA256,TLS_DHE_RSA_WITH_AES_256_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,TLS_DHE_RSA_WITH_AES_256_GCM_SHA384}")
	private String cipherSuitesString;
	@Value("${servlet.security.protocols:TLSv1.2}")
	private String protocolsString;
	@Value("${servlet.service.classes:}")
    private String serviceClasses;

    /**
     * 默认的构造函数
     */
    public ServletServerConfigBean() {
        super(ServerType.Servlet);
    }

	/**
	 * {@inheritDoc}
	 * 
	 * @see HttpServerConfigBean#getCipherSuites()
	 */
	public String[] getCipherSuites() {
		return StringUtils.split(cipherSuitesString, ",", true, true);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see HttpServerConfigBean#getProtocols()
	 */
	public String[] getProtocols() {
		return StringUtils.split(protocolsString, ",", true, true);
	}

    /**
     * {@inheritDoc}
     *
     * @see HttpServerConfigBean#getOutputSize()
     */
    public long getOutputSize() {
        return TypeUtils.string2Size(outputSizeString, 32 * 1024);
    }

    /**
     * {@inheritDoc}
     *
     * @see HttpServerConfigBean#getRequestHeaderSize()
     */
    public long getRequestHeaderSize() {
        return TypeUtils.string2Size(requestHeaderSizeString, 8 * 1024);
    }

    /**
     * {@inheritDoc}
     *
     * @see HttpServerConfigBean#getResponseHeaderSize()
     */
    public long getResponseHeaderSize() {
        return TypeUtils.string2Size(responseHeaderSizeString, 8 * 1024);
    }

    /**
     * 获取配置的Servlet服务类列表
     *
     * @return Servlet服务类类别
     */
    public String[] getServiceClasses() {
        return StringUtils.split(serviceClasses);
    }
}
